<?php

/*
 * 这不是一个开放软件，您不能用于商业应用
 * 不允许对程序代码以任何形式任何目的的再发布。
 * Copyright (c) 2015 YOUM.Co.Ltd. All rights reserved..
 * 网站地址：http://www.huishuojs.com
 * Author Zhyu
 */

/**
 * @author Zhyu
 * @date 2015-12-20 02:21:04
 * @copyright 杭州晖硕有码科技
 */
class User_level extends PC_Controller{
    //put your code here
    protected $go_url = '/admin/user_level/index';
    public function __construct(){
        parent::__construct();
        $this->load->model('user_level_model');
    }
    /**
     * 列表
     */
    public function index(){
        $this->load->view('/admin/user_level_list.php',  $this->data);
    }
    /**
     * 
     */
    public function ajax_user_level_list(){
        $data = $_GET;
        $start = $data['iDisplayStart']; //显示的起始索引
        $length = $data['iDisplayLength'];//显示的行数
        $sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列
        $sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
        $where=array();
        /*$search = $data ['sSearch'];//全局搜索字段
        $data['username'] ? $where['username like'] = '%'.trim($data['username']).'%' :'';
        $data['phone'] ? $where['phone like'] = '%'.trim($data['phone']).'%' :'';
        $data['parent_id']?$where['parent_id']=$data['parent_id']:'';
        $where['concat(id) like'] = '%'.trim($search).'%';*/
        $this->data['count'] = $this->user_level_model->getCount($where);
        $aaData = array();
        if($this->data['count']) {
                $order_by = $sort_th." ".$sort_type;
                $user_level_list = $this->user_level_model->getUserLevelList($where,$length,$start,$order_by);
                //echo $this->db->last_query();
                foreach($user_level_list as $key => $value) {
                    if($value['is_default']=='1'){
                       $user_level_list[$key]['is_default']='是'; 
                    }else{
                        $user_level_list[$key]['is_default']='否';
                    }
                    $del_oper = "onclick=\"if(confirm('确实要删除此条记录吗？')) return true;else return false; \"";
                    $edit_url = '<a href="/admin/user_level/edit_page/'.$value['id'].'"><span class="btn btn-xs btn-primary">编辑</span></a>';
                    $del_url = '<a href="/admin/user_level/del/'.$value['id'].'"  '.$del_oper.'><span class="btn btn-xs btn-danger cancel">删除</span></a>';
                    $user_level_list[$key]['operate'] = $edit_url." ".$del_url;
                }
                $aaData = $user_level_list;

        }
        $output['aaData'] = $aaData;
        $output['sEcho'] = $_GET['sEcho'];
        $output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
        $output['iTotalRecords'] = $this->data['count']; //总共有几条数据
        echo json_encode($output); //最后把数据以json格式返回
	}
        /**
	 * @brief 进入编辑页面
	 * @param Null
	 * @par 2015/10/12 Ver 1.00 Created by Allen
	 */
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$user_level_info = $this->user_level_model->checkUserLevel($where);
		
		$this->data['data'] = $user_level_info;
		$this->load->view('/admin/user_level_edit',$this->data);
	}
        /**
	 * @brief 保存编辑信息
	 * @param Null
	 * @par 2015/10/12 Ver 1.00 Created by Allen
	 */
	public function edit() {
            $data = $_POST;
            $where['id'] = $data['id'];
            if($data['is_default']=='1 '){
                $this->user_level_model->editUserLevel(array(),array('is_default'=>'0'));
            }
            $edit_result = $this->user_level_model->editUserLevel($where,$data);
            
	   if($edit_result) {
			$this->location_href($this->go_url."/2");	
		}else {
			$this->location_href($this->go_url."/3");	
		}
	}
	
	/**
	 * @brief 删除会员信息
	 * @param Null
	 * @par 2015/10/12 Ver 1.00 Created by Allen
	 */
	public function del() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;	
		$del_result = $this->user_level_model->deleteUserLevel($where);
		$go_url = '/admin/user/user_list';
	   if($edit_result) {
			$this->location_href($this->go_url."/2");	
		}else {
			$this->location_href($this->go_url."/3");	
		}
	}
        /**
	 * @brief 检查管理员姓名唯一性
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function check_sole() {
		$data = $_POST;
		if(isset($data['id'])) {
			$where['id !='] = $data['id'];
		}
		$where['other_performance'] = $data['other_performance'];
                $where['group_performance'] = $data['group_performance'];
		$sole = $this->user_level_model->checkUserLevel($where);
		if($sole) {
			$message = false;
		}else {
			$message = true;
		}
		echo json_encode($message);
	}
        /**
	 * @brief 进入添加页面
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function add_page() {
		$this->load->view('/admin/user_level_add',$this->data);
	}
	
	/**
	 * @brief 保存添加信息
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function add() {
		$data = $_POST;	
		$add_result = $this->user_level_model->addUserLevel($data);
		if($add_result) {
			$this->location_href($this->go_url."/4");
		}else {
			$this->location_href($this->go_url."/5");
		}
	}	
    
}
